﻿#include"Tree.h"

BTNode* BuyNode(char x)
{
	BTNode* newnode = (BTNode*)malloc(sizeof(BTNode));

	newnode->data = x;
	newnode->left = newnode->right = NULL;

	return newnode;
}

BTNode* createTree()
{
	BTNode* nodeA = BuyNode('A');
	BTNode* nodeB = BuyNode('B');
	BTNode* nodeC = BuyNode('C');
	BTNode* nodeD = BuyNode('D');
	BTNode* nodeE = BuyNode('E');
	BTNode* nodeF = BuyNode('F');

	nodeA->left = nodeB;
	nodeA->right = nodeC;
	nodeB->left = nodeD;
	nodeC->left = nodeE;
	nodeC->right = nodeF;

	return nodeA;
}

void test01()
{
	BTNode* nodeA = BuyNode('A');
	BTNode* nodeB = BuyNode('B');
	BTNode* nodeC = BuyNode('C');
	BTNode* nodeD = BuyNode('D');
	BTNode* nodeE = BuyNode('E');
	BTNode* nodeF = BuyNode('F');

	nodeA->left = nodeB;
	nodeA->right = nodeC;
	nodeB->left = nodeD;
	nodeC->left = nodeE;
	nodeC->right = nodeF;

	PreOrder(nodeA);
	printf("\n");
	InOrder(nodeA);
	printf("\n");
	PostOrder(nodeA);
	printf("\n");
}

void test02()
{
	BTNode* root = createTree();

	// ⼆叉树结点个数 -- 有效结点个数
	size_t ret1 = BinaryTreeSize(root);
	printf("size:%zu\n", ret1);

	// ⼆叉树叶⼦结点个数
	size_t ret2 = BinaryTreeLeafSize(root);
	printf("leaf size:%zu\n", ret2);

	// ⼆叉树第k层结点个数 
	size_t ret3 = BinaryTreeLevelKSize(root, 3);
	printf("K Level Size:%zu\n", ret3);

	// ⼆叉树的深度/⾼度
	size_t ret4 = BinaryTreeDepth(root);
	printf("Tree Depth:%zu\n", ret4);

	// ⼆叉树查找值为x的结点 
	BTNode* find = BinaryTreeFind(root, 'E');
	if (find)
	{
		printf("找到了!\n");
	}
	else
	{
		printf("未找到!\n");
	}

	//层序遍历
	printf("层序遍历的结果: ");
	LevelOrder(root);

	//判断是否为完全二叉树
	bool isComplete = BinaryTreeComplete(root);
	if (isComplete)
	{
		printf("是完全二叉树\n");
	}
	else
	{
		printf("不是完全二叉树\n");
	}
	
	// ⼆叉树销毁
	BinaryTreeDestory(&root);
}

int main()
{
	//test01();
	test02();
	return 0;
}